package com.have.blog.admin.redis.impl;

import com.have.blog.admin.constant.AdminRedisConstant;
import com.have.blog.admin.constant.RedisExpiredTime;
import com.have.blog.admin.domain.dto.ResourceMenuDto;
import com.have.blog.admin.redis.ResourceMenuCacheService;
import com.have.blog.config.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created By Have
 * 2021/7/17 21:18
 */
@Service
public class ResourceMenuCacheServiceImpl implements ResourceMenuCacheService {
    @Value("${redis.admin.database}")
    private String REDIS_DATABASE;
    @Autowired
    private RedisService redisService;

    @Override
    public void setHierarchyList(List<ResourceMenuDto> resourceMenus) {
        String key = REDIS_DATABASE + AdminRedisConstant.SEGMENTATION
                + AdminRedisConstant.HIERARCHY_RESOURCE_LIST;
        redisService.set(key, resourceMenus, RedisExpiredTime.ONE_DAY.getDuration(), RedisExpiredTime.ONE_DAY.getTimeUnit());
    }

    @Override
    public void delHierarchyList() {
        String key = REDIS_DATABASE + AdminRedisConstant.SEGMENTATION
                + AdminRedisConstant.HIERARCHY_RESOURCE_LIST;
        redisService.del(key);
    }

    @Override
    public List<ResourceMenuDto> getHierarchyList() {
        String key = REDIS_DATABASE + AdminRedisConstant.SEGMENTATION
                + AdminRedisConstant.HIERARCHY_RESOURCE_LIST;
        return (List<ResourceMenuDto>) redisService.get(key);
    }

    @Override
    public void delButtons() {
        String key = REDIS_DATABASE + AdminRedisConstant.SEGMENTATION
                + AdminRedisConstant.BUTTONS;
        redisService.del(key);
    }

    @Override
    public List<ResourceMenuDto> getButtonsWitParent() {
        String key = REDIS_DATABASE + AdminRedisConstant.SEGMENTATION
                + AdminRedisConstant.BUTTONS;
        return (List<ResourceMenuDto>) redisService.get(key);
    }

    @Override
    public void setButtonsWithParent(List<ResourceMenuDto> buttonParents) {
        String key = REDIS_DATABASE + AdminRedisConstant.SEGMENTATION
                + AdminRedisConstant.BUTTONS;
        redisService.set(key, buttonParents, RedisExpiredTime.ONE_DAY.getDuration(), RedisExpiredTime.ONE_DAY.getTimeUnit());
    }
}
